#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, request, render_template
from tools import *

app = Flask(__name__)


@app.route("/")
def play():
    return render_template('play.html')


@app.route('/gens')
def gens():
    je = json.JSONEncoder()
    gens = os.listdir('train_net')
    gens.sort(key=lambda x: int(x), reverse=True)
    return je.encode(gens)


@app.route("/next", methods=['POST'])
def next():
    gen = request.form['gen']
    vec = request.form['input']
    color = request.form['color']

    com = joblib.load(os.path.join('train_net', gen, '0.net'))
    if color == '7':
        com.color = 1
    else:
        com.color = 7

    vec = json2array(vec)

    win = winner(vec)
    if win == 0:
        vec = com.next(vec)
        win = winner(vec)

    vec = vec.reshape(225).tolist()
    res = {'next': vec, 'win': win}
    je = json.JSONEncoder()
    return je.encode(res)
